package com.xaicode.template.activemq.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;


public class JMS_producer_topic_transfer {

    public static final String ACTIVEMQ_URL = "tcp://192.168.18.182:61616";
    public static final String TOPIC_NAME = "CRAE_REPORT_TOPIC";


    /**
     * 1、一次运行订阅此公众号 注册一次 然后关闭程序
     * 2、再次运行依然可以收到持久化消息
     *
     * @param args
     * @throws JMSException
     */
    public static void main(String[] args) throws JMSException {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        Connection connection = activeMQConnectionFactory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(TOPIC_NAME);
        MessageProducer producer = session.createProducer(topic);
        producer.setDeliveryMode(DeliveryMode.PERSISTENT);
        connection.start();
        String msg = createReqMsg();
//        Map<String,Object> map = new HashMap<>();
//        map.put("Msg",msg);
        MapMessage mapMessage = session.createMapMessage();
        mapMessage.setObject("Msg", msg);
        producer.send(mapMessage);
        producer.close();
        session.close();
        connection.close();

        System.out.println("持久化TOPIC消息发送完成");
    }

    public static String createReqMsg() {
        String msg = "{IEBeltOffset:{\"Pos\":10,\"IeType\":56,\"Offset\":13}, IEMsgHeader:{\"MsgType\":\"CRZS_BELT_STATUS_PUSH_REQ\",\"MsgSn\":1,\"IeType\":1}, IEBelt:{\"Speed\":0,\"MaxSpeed\":0,\"Len\":0,\"Id\":\"600070009\",\"Width\":0,\"IeType\":55,\"Name\":\"\"}}";
        return msg;
    }
}
